﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;


namespace Ahi.Base
{
    public class IdFormParameter : FormParameter
    {
        public const string name = "_id";

        public override string Name
        {
            get
            {
                return name;
            }
            set
            {
                base.Name = value;
            }
        }

        public override bool HasValue
        {
            get
            {
                return base.HasValue && AsObjectId != ObjectId.Empty;
            }
        }

        public static implicit operator IdFormParameter(ObjectId id)
        {
            return new IdFormParameter { Value = id.ToString() };
        }

        public ObjectId AsObjectId
        {
            get
            {
                ObjectId objId = ObjectId.Empty;
                if (ObjectId.TryParse(ValueAsString, out objId))
                    return objId;
                return ObjectId.Empty;
            }
        }
    }
}
